<?php

class Application_Model_DbTable_ZFNews extends Zend_Db_Table_Abstract {

    protected $_name = 'zf_news';
    protected $country = 'GB';
    protected $language = 'en';

    public function setLanguage($lang) {
        if ($lang != "") {
            $this->language = $lang;
        }
    }

    public function setCountry($count) {
        if ($count != "") {
            $this->country = $count;
        }
    }

    private function getAvNewsIdHash() {
        $select = $this->_db->select()
                        ->from('zf_news_country', array('news_id', 'news_id'))
                        ->where('code_country = ?', $this->country)
                        ->order('news_id ASC');
        $resultsa = $this->getAdapter()->fetchPairs($select);
        return array_reverse($resultsa);
    }

    public function getAllNews() {
        $resultsa = $this->getAvNewsIdHash();
        #Zend_Debug::dump($resultsa, '$resultSA', true);
        $allnews = array();
        $counter = 0;
        if (is_array($resultsa)) {
            foreach ($resultsa as $key => $value) {
                $select = $this->_db->select()
                                ->from($this->_name, array('news_id', 'title', 'short_news', 'long_news', 'dest_country', 'pub_date', 'admin_id', 'picture', 'published', 'insert_time'))
                                ->where('news_id = ?', $value)
                                ->where('dest_lang = ?', $this->language)
                                ->where('published = ?', 1);
                $results = $this->getAdapter()->fetchAll($select);
                # Zend_Debug::dump($results, '$results', true);
                if (is_array($results)) {
                    if (count($results) > 0) {
                        $result = $results[0];
                        if (is_array($result)) {
                            $allnews[$counter] = $result;
                            $counter++;
                        }
                    }
                }
            }
        }
        #    Zend_Debug::dump($allnews, '$allnews', true);
        return $allnews;
    }

    public function publishNews($newsArray) {
        foreach ($newsArray as $id => $value) {
            $q_sessions = "UPDATE zf_news SET published = $value WHERE news_id = $id";
            $this->_db->query($q_sessions);
        }
    }

    public function getLatestNewsForFooter() {
        $resultsa = $this->getAllNews();
        if (is_array($resultsa)) {
            if (count($resultsa) > 0) {
                return $resultsa[0]["short_news"];
            } else {
                return "";
            }
        } else {
            return "";
        }
    }

    public function getAllArticlesForCountry($country) {
        $select = $this->_db->select()
                        ->from('zf_news_country', array('news_id', 'code_country'))
                        ->where('code_country = ?', $country);
        $results = $this->getAdapter()->fetchPairs($select);
        $allresult = array();
        $counter = 0;
        if (is_array($results)) {
            foreach ($results as $id => $countryCode) {
                $select = $this->_db->select()
                                ->from($this->_name, array('news_id', 'title', 'short_news', 'long_news', 'dest_lang', 'pub_date', 'picture', 'published'))
                                ->where('news_id = ?', $id);
                $temp_result = $this->getAdapter()->fetchAll($select);
                $allresult[$counter] = $temp_result[0];
                $counter++;
            }
        }
        return $allresult;
    }

    public function getAllArticlesByIds($artIds) {
        $articles = array();
        $counter=0;
        foreach ($artIds as $id => $country) {
            $select = $this->_db->select()
                            ->from($this->_name, array('news_id', 'title', 'short_news', 'long_news', 'dest_lang', 'pub_date', 'picture', 'published'))
                            ->where('news_id = ?', $id);
            $temp_result = $this->getAdapter()->fetchAll($select);
            $articles[$counter] = $temp_result[0];
            $counter++;
        }
        return $articles;
    }

    public function getArticlesIdsForCountries($countries) {
        $resultx = array();
        foreach ($countries as $key => $value) {
            $select = $this->_db->select()
                            ->from('zf_news_country', array('news_id', 'code_country'))
                            ->where('code_country = ?', $value['code_country']);
            $results = $this->getAdapter()->fetchPairs($select);
#            Zend_Debug::dump($results, '$results', true);
            foreach ($results as $id => $code) {
                $resultx[$id] = $code;
            }
            #$resultx = array_merge($resultx, $results);
        }
#        Zend_Debug::dump($resultx, 'resultx001', true);
        foreach ($countries as $key => $value) {
            $select = $this->_db->select()
                            ->from('zf_news', array('news_id', 'dest_country'))
                            ->where('dest_country = ?', $value['code_country']);
            $results = $this->getAdapter()->fetchPairs($select);
            #           Zend_Debug::dump($results, '$results', true);
            foreach ($results as $id => $code) {
                $resultx[$id] = $code;
            }
            #$resultx = array_merge($resultx, $results);
        }
        return $resultx;
#        Zend_Debug::dump($resultx, 'resultx002', true);
    }

}